package com.party.core.service.crowdfund.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.crowdfund.ProjectGradationReadDao;
import com.party.core.dao.write.crowdfund.ProjectGradationWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.dao.write.crowdfund.ProjectGradationWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.crowdfund.EventGradation;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.crowdfund.ProjectGradation;
import com.party.core.service.crowdfund.IProjectGradationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: huangzequn
 * @Date: 2019/3/20 9:44
 */
@Service
public class ProjectGradationService implements IProjectGradationService {

    @Autowired
    private ProjectGradationReadDao projectGradationReadDao;

    @Autowired
    private ProjectGradationWriteDao projectGradationWriteDao;


    @Override
    public String insert(ProjectGradation projectGradation) {
        BaseModel.preInsert(projectGradation);
        boolean result = projectGradationWriteDao.insert(projectGradation);
        if (result) {
            return projectGradation.getId();
        }
        return null;
    }

    @Override
    public boolean update(ProjectGradation projectGradation) {

        if (null != projectGradation.getUpdateDate()) {
            projectGradation.setUpdateDate(new Date());
        }
        return projectGradationWriteDao.update(projectGradation);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return projectGradationWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {

        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return projectGradationWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return projectGradationWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return projectGradationWriteDao.batchDelete(ids);
    }

    @Override
    public ProjectGradation get(String id) {
        return projectGradationReadDao.get(id);
    }

    @Override
    public List<ProjectGradation> listPage(ProjectGradation projectGradation, Page page) {

        return projectGradationReadDao.listPage(projectGradation, page);

    }

    @Override
    public List<ProjectGradation> list(ProjectGradation projectGradation) {

        return projectGradationReadDao.listPage(projectGradation, null);
    }

    @Override
    public List<ProjectGradation> batchList(Set<String> ids, ProjectGradation projectGradation, Page page) {
        return null;
    }

    /***
     * 多场活动个人排行榜，财富，人气
     * @param page
     * @return
     */
    @Override
    public List<ProjectGradation> speedList(Map<String, String> params, Page page) {
        return projectGradationReadDao.speedList(params, page);
    }

    /***
     * 个人排行榜  自己的个人信息
     * @param
     * @return
     */
    @Override
    public ProjectGradation popularity(Map<String, String> params, String type) {
        return projectGradationReadDao.popularity(params, type);
    }

    /***
     * 查询该项目下的所有报名人员
     * @param targetId
     * @param page
     * @return
     */
    @Override
    public List<Project> getProjectByActivityId(String targetId, Page page) {
        return projectGradationReadDao.getProjectByActivityId(targetId,page);
    }

    /***
     * 查询该主题下的所有报名人员
     * @param eventId
     * @param page
     * @return
     */
    @Override
    public List<Project> queryProjectByTargetId(String eventId, Page page) {
        return projectGradationReadDao.queryProjectByTargetId(eventId,page);
    }

    /**
     * * 找出所有报名者已消费的总金额，比如说当前系统时间是第三阶段，则需要找出第一第二阶段的目标金额
     *  并且加条件过滤出来   第一第二阶段标记已参赛的  或者 有阶段筹满时间的
     * @param params
     * @return
     */
    @Override
    public List<ProjectGradation> getProjectGradationByTargetId(Map<String, Object> params) {
        return projectGradationReadDao.getProjectGradationByTargetId(params);
    }


    /***
     * 根据eventGradationId找到ProjectGradation
     * @param eventGradationId
     * @return
     */
    @Override
    public ProjectGradation getByEventGradationId(String eventGradationId,String projectId) {
        return projectGradationReadDao.getByEventGradationId(eventGradationId,projectId);
    }


    @Override
    public List<Map<String, Object>> listForProjectId(String projectId) {
        return projectGradationReadDao.listForProjectId(projectId);
    }

    @Override
    public ProjectGradation getUnique(ProjectGradation projectGradation) {
        return projectGradationReadDao.getUnique(projectGradation);
    }

    @Override
    public Double getEffectiveAmount(Date endTime, String projectId) {
        return projectGradationReadDao.getEffectiveAmount(endTime, projectId);
    }

    @Override
    public Double getConsumedAmount(String eventId) {
        return projectGradationReadDao.getConsumedAmount(eventId);
    }

    @Override
    public List<ProjectGradation> getSuccessListForProjectId(String projectId) {
        return projectGradationReadDao.getSuccessListForProjectId(projectId);
    }

    @Override
    public List<ProjectGradation> findByProjectId(String projectId) {
        return projectGradationReadDao.findByProjectId(projectId);
    }

    @Override
    public Integer getEffectiveCount(String projectId) {
        return projectGradationReadDao.getEffectiveCount(projectId);
    }

    /**
    * @Author hzq
    * @Description //获取当前众筹者正在众筹阶段的后面所有阶段
    * @Date 15:31 2019/4/18
    * @param ongoingGradation
    * @return
    **/
    @Override
    public List<ProjectGradation> getAfterProjectGradations(EventGradation ongoingGradation,String projectId) {
        return projectGradationReadDao.getAfterProjectGradations(ongoingGradation,projectId);
    }

    /**
    * @Author hzq
    * @Description //根据当前阶段找到上一阶段
    * @Date 15:49 2019/4/18
    * @param ongoingProjectGradation
    * @return
    **/
    @Override
    public ProjectGradation getTheNextGradation(ProjectGradation ongoingProjectGradation) {
        return projectGradationReadDao.getTheNextGradation(ongoingProjectGradation);
    }
}
